import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/widgets/chart/kq_chart.dart';
import 'package:kq_flutter_core_widget/widgets/chart/pie/pie_chart.dart';
import 'package:get/get.dart';

/// 饼状图
class PieChart3DPage extends StatelessWidget {
  const PieChart3DPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('3D饼状图'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          color: Colors.white,
          onPressed: Get.back,
        ),
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart2(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart3(),
            ),
          ),
          const SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: Text(
                "下面为两种无数据的展示方式：",
                style: TextStyle(fontSize: 20),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar2(),
            ),
          )
        ],
      ),
    );
  }

  /// 常规饼状图
  Widget buildPieChart() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChart3DDelegate(
          animDuration: const Duration(seconds: 2),
          radius: 90,
          height: 30,
          dataRender: PieChart3DDataRender(
              highLightRender: PieChart3DHighLightRender(
                  offsetRadius: 0,
                  colorBuilder: (p0) {
                    return HSLColor.fromColor(p0.color)
                        .withLightness(0.7)
                        .toColor();
                  })),
          labelRender: const PieChart3DLabelRender(),
          gestureHandler: PieChart3DGestureHandler(),
          data: [
            PieChartEntity(
                color: Colors.redAccent,
                value: 10,
                drawLabel: true,
                label: "测试",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试2",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.blueAccent,
                value: 33.5,
                drawLabel: true,
                label: "测试1",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试12",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.brown,
                value: 10,
                drawLabel: true,
                label: "测试2",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试22",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.lightGreen,
                value: 19.3,
                drawLabel: true,
                label: "测试3",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试32",
                secondLabelSize: 8)
          ],
          labelPosition: LabelPosition.inside(),
          sliceSpace: 0,
          innerRadius: 0,
        ),
      ),
    );
  }

  /// 圆环饼状图
  Widget buildPieChart2() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChart3DDelegate.withDefault(
          animDuration: const Duration(seconds: 2),
          radius: 90,
          height: 30,
          data: [
            PieChartEntity(
                color: Colors.redAccent,
                value: 10,
                drawLabel: true,
                label: "测试",
                labelSize: 10,
                labelColor: Colors.redAccent,
                secondLabel: "测试2",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.blueAccent,
                value: 33.5,
                drawLabel: true,
                label: "测试1",
                labelSize: 10,
                labelColor: Colors.blueAccent,
                secondLabel: "测试12",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.brown,
                value: 10,
                drawLabel: true,
                label: "测试2",
                labelSize: 10,
                labelColor: Colors.brown,
                secondLabel: "测试22",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.lightGreen,
                value: 19.3,
                drawLabel: true,
                label: "测试3",
                labelSize: 10,
                labelColor: Colors.lightGreen,
                secondLabel: "测试32",
                secondLabelSize: 8)
          ],
          labelPosition: LabelPosition.outside(
            lineFactory: (entity) {
              return IndicatorLine(
                  color: entity.color,
                  drawStartCircle: true,
                  circleColor: entity.color,
                  offset: 4);
            },
          ),
          dataRender: const PieChart3DDataRender(
            highLightRender: PieChart3DHighLightRender()
          ),
          sliceSpace: 4,
          innerRadius: 60,
        ),
      ),
    );
  }

  /// 丁达尔玫瑰图
  Widget buildPieChart3() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChart3DDelegate(
          animDuration: const Duration(seconds: 2),
          radius: 90,
          height: 30,
          dataRender: const PieChart3DDataRender(
              highLightRender:
                  PieChart3DHighLightRender(addedRadius: 20, offsetRadius: 0)),
          gestureHandler: PieChart3DGestureHandler(),
          data: [
            PieChartEntity(color: Colors.redAccent, value: 10),
            PieChartEntity(color: Colors.brown, value: 15, addedRadius: 10),
            PieChartEntity(
                color: Colors.lightGreen, value: 20, addedRadius: 20),
            PieChartEntity(
                color: Colors.blueAccent, value: 25, addedRadius: 30),
          ],
          sliceSpace: 0,
          innerRadius: 60,
        ),
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar() {
    return Container(
      width: 160,
      height: 160,
      color: Colors.lightBlue[200],
      child: KqChart(
        delegate: KqPieChart3DDelegate.withDefault(radius: 70),
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar2() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChart3DDelegate(
          radius: 90,
          innerRadius: 60,
          baseMapColor: Colors.grey,
          height: 30,
          dataRender: const PieChart3DDataRender(),
          gestureHandler: PieChart3DGestureHandler(),
        ),
      ),
    );
  }
}
